home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
TEMP
/
GNU
/
bison
/
Symbols
< prev
next >
Wrap
Text File
|
1995-06-28
|
4KB
|
79 lines
Symbols
Previous: <Grammar Outline=>GrammarOut> * Next: <Rules=>Rules> * Up: <Grammar File=>GrammarFil>
#Wrap on
{fH3}Symbols, Terminal and Nonterminal{f}
{fUnderline}Symbols{f} in Bison grammars represent the grammatical classifications
of the language.
A {fUnderline}terminal symbol{f} (also known as a {fUnderline}token type{f}) represents a
class of syntactically equivalent tokens. You use the symbol in grammar
rules to mean that a token in that class is allowed. The symbol is
represented in the Bison parser by a numeric code, and the {fCode}yylex{f}
function returns a token type code to indicate what kind of token has been
read. You don't need to know what the code value is; you can use the
symbol to stand for it.
A {fUnderline}nonterminal symbol{f} stands for a class of syntactically equivalent
groupings. The symbol name is used in writing grammar rules. By convention,
it should be all lower case.
Symbol names can contain letters, digits (not at the beginning),
underscores and periods. Periods make sense only in nonterminals.
There are two ways of writing terminal symbols in the grammar:
#Indent +4
• A {fUnderline}named token type{f} is written with an identifier, like an
identifier in C. By convention, it should be all upper case. Each
such name must be defined with a Bison declaration such as
{fCode}%token{f}. \*Note <Token Decl=>TokenDecl>: Token Type Names.
• A {fUnderline}character token type{f} (or {fUnderline}literal token{f}) is written in
the grammar using the same syntax used in C for character constants;
for example, {fCode}'+'{f} is a character token type. A character token
type doesn't need to be declared unless you need to specify its
semantic value data type (\*Note <Value Type=>ValueType>: Data Types of Semantic Values), associativity, or
precedence (\*Note <Precedence=>Precedencf>: Operator Precedence).
By convention, a character token type is used only to represent a
token that consists of that particular character. Thus, the token
type {fCode}'+'{f} is used to represent the character {fEmphasis}+{f} as a
token. Nothing enforces this convention, but if you depart from it,
your program will confuse other readers.
All the usual escape sequences used in character literals in C can be
used in Bison as well, but you must not use the null character as a
character literal because its ASCII code, zero, is the code
{fCode}yylex{f} returns for end-of-input (\*Note <Calling Convention=>CallingCon>: Calling Convention for {fCode}yylex{f}).
#Indent
How you choose to write a terminal symbol has no effect on its
grammatical meaning. That depends only on where it appears in rules and
on when the parser function returns that symbol.
The value returned by {fCode}yylex{f} is always one of the terminal symbols
(or 0 for end-of-input). Whichever way you write the token type in the
grammar rules, you write it the same way in the definition of {fCode}yylex{f}.
The numeric code for a character token type is simply the ASCII code for
the character, so {fCode}yylex{f} can use the identical character constant to
generate the requisite code. Each named token type becomes a C macro in
the parser file, so {fCode}yylex{f} can use the name to stand for the code.
(This is why periods don't make sense in terminal symbols.)
\*Note <Calling Convention=>CallingCon>: Calling Convention for {fCode}yylex{f}.
If {fCode}yylex{f} is defined in a separate file, you need to arrange for the
token-type macro definitions to be available there. Use the {fEmphasis}-d{f}
option when you run Bison, so that it will write these macro definitions
into a separate header file {fCite}{fStrong}name{f}.tab.h{f} which you can include
in the other source files that need it. \*Note <Invocation=>Invocation>: Invoking Bison.
The symbol {fCode}error{f} is a terminal symbol reserved for error recovery
(\*Note <Error Recovery=>ErrorRecov>); you shouldn't use it for any other purpose.
In particular, {fCode}yylex{f} should never return this value.